package org.wxd.springcloud.swagger.ctl;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.assertj.core.util.Lists;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.wxd.springcloud.swagger.ctl.vo.ResponseVO;
import org.wxd.springcloud.swagger.ctl.vo.demo.file.FileRequest;
import org.wxd.springcloud.swagger.ctl.vo.demo.get.GetDemoRequest;
import org.wxd.springcloud.swagger.ctl.vo.demo.get.GetDemoResponse;
import org.wxd.springcloud.swagger.ctl.vo.demo.post.PostDemoRequest;
import org.wxd.springcloud.swagger.ctl.vo.demo.post.PostDemoResponse;
import org.wxd.springcloud.swagger.ctl.vo.demo.post.PostJsonDemoRequest;
import org.wxd.springcloud.swagger.ctl.vo.demo.post.PostJsonDemoResponse;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Comparator;
import java.util.List;

/**
 * 在2.7.0版本可以使用@ApiModelProperties 可以解决example、hidden、需要额外的注解等问题
 * <p>
 * 2.2.2老版本的限制：
 * @ApiIgnore 可以作用于方法参数，表示忽略该参数
 * @ApiParam 用于Get请求或Post请求，不可以作用于json请求的入参，可以作用于类里的字段，但很多属性无效，建议直接使用@ApiImplicitParams注解方法上使用
 * @ApiModelProperties 主要作用于json请求的出入参，也可以忽略某一个入参
 * @ApiImplicitParams 只可以作用于方法的入参 且必需与@ApiImplicitParam 组合用
 * @ApiImplicitParam 指定请求类型：paramType 值有：路径参数 path; get入参 query; json入参 body; header入参 header; 表单 form;
 * 数据类型：
 * 注意：如果Get请求或者Post请求 入参数用类包裹，则需要加上@ModelAttribute 注解
 */
@Api("老版本demo")
@RequestMapping(value = "/demo")
@RestController
public class DemoController {


// 带header

    // GET 请求

    // 普通get请求
    @ApiOperation("get请求")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "id", value = "id", dataType = "string", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "get.name", value = "内置对象", dataType = "string", defaultValue = "内置"),
            @ApiImplicitParam(paramType = "query", name = "name", value = "姓名", dataType = "string", defaultValue = "某"),
            @ApiImplicitParam(paramType = "query", name = "certs", value = "证件列表", dataType = "string", defaultValue = "2,3", allowMultiple = true)
    })
    @GetMapping(value = "/header/getDemo/{id}")
    public ResponseVO<GetDemoResponse> getDemo(@PathVariable("id") String id, @ApiIgnore GetDemoRequest request) throws InterruptedException {

        System.out.println(id);

        Thread.sleep(Integer.valueOf(id) * 1000);

        List<GetDemoRequest> requests = Lists.newArrayList();
        List<String> s = requests.stream().max(Comparator.comparingInt(GetDemoRequest::hashCode)).map(GetDemoRequest::getCerts).orElse(Lists.newArrayList());
        System.out.println(s);

        return ResponseVO.success(new GetDemoResponse());
    }

    // POST 请求
    // 表单请求
    @ApiOperation("表单请求")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "form", name = "id", value = "id", dataType = "string", defaultValue = "1"),
            @ApiImplicitParam(paramType = "form", name = "name", value = "姓名", dataType = "string", defaultValue = "某"),
            @ApiImplicitParam(paramType = "form", name = "post.id", value = "内嵌对象", dataType = "string", defaultValue = "内置对象"),
            @ApiImplicitParam(paramType = "form", name = "certs", value = "证件列表", dataType = "string", defaultValue = "2,3", allowMultiple = true),
    })
    @PostMapping(value = "/header/post")
    public ResponseVO<PostDemoResponse> post(@ApiIgnore PostDemoRequest request) {
        return ResponseVO.success(new PostDemoResponse());
    }

    // json请求
    @ApiOperation("json请求")
    @PostMapping(value = "/header/postWithJson")
    public ResponseVO<List<PostJsonDemoResponse>> post(@RequestBody PostJsonDemoRequest request) {
        return ResponseVO.success(new PostJsonDemoResponse());
    }


    // 文件上传
    @ApiOperation("文件上传")
    @PostMapping(value = "/header/file")
    public ResponseVO file(@RequestParam("pic") MultipartFile file, @ModelAttribute FileRequest request) {
        return ResponseVO.success(null);
    }

// 不带header

    // 普通get请求
    @ApiOperation("get请求")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "id", value = "id", dataType = "string", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "get.name", value = "内置对象", dataType = "string", defaultValue = "内置"),
            @ApiImplicitParam(paramType = "query", name = "name", value = "姓名", dataType = "string", defaultValue = "某"),
            @ApiImplicitParam(paramType = "query", name = "certs", value = "证件列表", dataType = "string", defaultValue = "2,3", allowMultiple = true),
    })
    @GetMapping(value = "/noHeader/getDemo/{id}")
    public ResponseVO<GetDemoResponse> getUnHeader(@PathVariable("id") String id, @ApiIgnore GetDemoRequest request) {
        return ResponseVO.success(new GetDemoResponse());
    }
}
